package com.atom.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.atom.annotation.ApiImpl;

import com.atom.annotation.api.ApiImplCallback;
import com.atom.annotation.api.ApiImplContext;
import com.atom.app.base.AbstractFragment;


@ApiImpl(api = AbstractFragment.class, name = "main/menu")
public class TestMenuFragment extends AbstractFragment {

    public TestMenuFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_main_menu, container, false);
        initView(inflate);
        return inflate;
    }

    void initView(View layout) {

        layout.findViewById(R.id.test_api).setOnClickListener(v -> {
            AbstractFragment apiByName = apiImplContext().getImpl(AbstractFragment.class, "main/menu/api", 0, false);
            if (apiByName != null) {
                loadFragment(apiByName, true);
            }
        });

        layout.findViewById(R.id.test_impl).setOnClickListener(v -> {
            AbstractFragment apiByName = apiImplContext().getImpl(AbstractFragment.class, "main/menu/impl", 0, false);
            if (apiByName != null) {
                loadFragment(apiByName, true);
            }
        });

        layout.findViewById(R.id.test_cache).setOnClickListener(v -> {
            AbstractFragment apiByName = apiImplContext().getImpl(AbstractFragment.class, "main/menu/cache", 0, false);
            if (apiByName != null) {
                loadFragment(apiByName, true);
            }
        });

        layout.findViewById(R.id.test_other).setOnClickListener(v -> {
            AbstractFragment apiByName = apiImplContext().getImpl(AbstractFragment.class, "main/menu/other", 0, false);
            if (apiByName != null) {
                loadFragment(apiByName, true);
            }
        });
        layout.findViewById(R.id.test_skip).setOnClickListener(v -> {
            apiImplContext().getApi(Activity.class, "data", new ApiImplCallback<Class<? extends Activity>>() {
                @Override
                public void call(ApiImplContext apiImplContext, Class<? extends Activity> aClass) {
                    startActivity(new Intent(requireActivity(), aClass));
                    requireActivity().finish();
                }
            });
        });
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);

    }
}
